from multiprocessing.connection import Client
from multiprocessing.reduction import recv_handle
import os
from socket import socket, AF_INET, SOCK_STREAM

def worker(server_address):
    serv = Client(server_address, authkey=b'peekaboo')
    serv.send(os.getpid())
    while True:
        fd = recv_handle(serv)
        print('WORKER: GOT FD', fd)
        with socket(AF_INET, SOCK_STREAM, fileno=fd) as client:
            while True:
                msg = client.recv(1024)
                if not msg:
                    break
                print('WORKER: RECV {!r}'.format(msg))
                client.send(msg)

if __name__ == '__main__':
    # import sys 
    # if len(sys.argv) != 2:
    #     print('Usage: worker.py server_address', file=sys.stderr)
    #     raise SystemExit(1)

    # worker(sys.argv[1])

    worker(r'\\.\pipe\myconn')

'''
unix:
python p11_socket_file_descriptor_between_processes2.py /tmp/servconn
windows:
python p11_socket_file_descriptor_between_processes2.py \\.\pipe\myconn
''' 

